//
//  XJWalletAddBankAccountViewController.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/5/23.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  添加银行卡账号

#import "XJWalletAddBankAccountViewController.h"
#import "XJLabelTextField.h"

@interface XJWalletAddBankAccountViewController ()

@property(nonatomic, strong) XJLabelTextField *nameTF; // 真实姓名
@property(nonatomic, strong) XJLabelTextField *bankNumberTF; // 卡号
@property(nonatomic, strong) XJLabelTextField *bankNameTF; // 银行名称
@property(nonatomic, strong) XJLabelTextField *kaiFuBankTF; // 开户支行


@property(nonatomic, strong) UIButton *submitButton;

@end

@implementation XJWalletAddBankAccountViewController

#pragma mark - 一 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navTitle = @"添加银行卡";
    [self.view addSubview:self.nameTF];
    [self.view addSubview:self.bankNumberTF];
    [self.view addSubview:self.bankNameTF];
    [self.view addSubview:self.kaiFuBankTF];
    [self.view addSubview:self.submitButton];
}

#pragma mark - 二 lazy
- (XJLabelTextField *)nameTF {
    if (!_nameTF) {
        _nameTF = [[XJLabelTextField alloc] initWithFrame:CGRectMake(15, 0, KWidth-15, 50) keyType:(UIKeyboardTypeDefault) textEntry:NO leftTitle:@"真实姓名" placeholder:@"请输入"];
    }
    return _nameTF;
}

- (XJLabelTextField *)bankNumberTF {
    if (!_bankNumberTF) {
        _bankNumberTF = [[XJLabelTextField alloc] initWithFrame:CGRectMake(15, 50, KWidth-15, 50) keyType:(UIKeyboardTypePhonePad) textEntry:NO leftTitle:@"银行卡号" placeholder:@"请输入"];
    }
    return _bankNumberTF;
}

- (XJLabelTextField *)bankNameTF {
    if (!_bankNameTF) {
        _bankNameTF = [[XJLabelTextField alloc] initWithFrame:CGRectMake(15, 100, KWidth-15, 50) keyType:(UIKeyboardTypeDefault) textEntry:NO leftTitle:@"银行名称" placeholder:@"请输入"];
    }
    return _bankNameTF;
}

- (XJLabelTextField *)kaiFuBankTF {
    if (!_kaiFuBankTF) {
        _kaiFuBankTF = [[XJLabelTextField alloc] initWithFrame:CGRectMake(15, 150, KWidth-15, 50) keyType:(UIKeyboardTypeDefault) textEntry:NO leftTitle:@"开户支行" placeholder:@"请输入"];
    }
    return _kaiFuBankTF;
}


- (UIButton *)submitButton {
    if (!_submitButton) {
        _submitButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        
        _submitButton.frame = CGRectMake(30, 300-30, KWidth - 60, 50);
        [_submitButton setTitle:@"提交" forState:(UIControlStateNormal)];
        [_submitButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        _submitButton.layer.masksToBounds = YES;
        _submitButton.layer.cornerRadius  = 10;
        _submitButton.backgroundColor = XJCOLOR_RGB(255, 216, 68);
        
        [_submitButton addTarget:self action:@selector(submitButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
    }
    
    return _submitButton;
}
- (void)submitButtonAction {
    XJLog(@"提交");
    
    // 判断
    NSString *card_name = self.nameTF.text; //持卡人姓名
    NSString *card_number = self.bankNumberTF.text; //银行卡号
    NSString *bank_name = self.bankNameTF.text; //银行名
    NSString *bank_branch = self.kaiFuBankTF.text; //开户支行
    if ([NSString isEmptyString:card_name]) {
        [JRToast showWithText:@"请输入姓名"];
        return;
    }
    
    if ([NSString isEmptyString:card_number]) {
        [JRToast showWithText:@"请输入银行卡号"];
        return;
    }
    if ([NSString isEmptyString:bank_name]) {
        [JRToast showWithText:@"请输入银行名称"];
        return;
    }
    if ([NSString isEmptyString:bank_branch]) {
        [JRToast showWithText:@"请输入开户支行"];
        return;
    }
    
    if (![NSString checkCardNo:card_number]) {
        [JRToast showWithText:@"未来巴扎:银行卡号长度16-20,且不能以0开头"];
        return;
    }
    // 组装请求参数
//    {
//        "bank_name": "工商银行",
//        "card_name": "嘻嘻",
//        "bank_branch": "亳州支行",
//        "type": "1",
//        "card_number": "6217002710000684874"
//    }

    NSNumber *type = @(1); //银行卡
    NSDictionary *parDic = NSDictionaryOfVariableBindings(card_name,bank_name,card_number,bank_branch,type);
    XJLog(@"添加银行卡请求参数:%@",parDic);
    
    [self requestAddAccountWithDic:parDic];
}


#pragma mark - 三 网络请求
- (void)requestAddAccountWithDic:(NSDictionary *)dic {
    [XJHttpTool requestCustomAccountAddAccountWithParDic:dic successH:^(id responseObjc) {
       
        NSString *code = [NSString stringWithFormat:@"%@",responseObjc[@"code"]];
        if ([code isEqualToString:@"0"]) {
            
            [JRToast showWithText:@"添加银行卡成功"];
            //返回
            [self.navigationController popViewControllerAnimated:YES];
        }else {
            [JRToast showWithText:responseObjc[@"message"]];
        }
    } failH:^(NSError *error) {
        [JRToast showWithText:[error localizedDescription]];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
